package cn.com.fattiger.framework.cms.admin.common.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.com.fattiger.framework.cms.admin.common.model.Menu;
import cn.com.fattiger.framework.cms.admin.common.service.MenuService;
import cn.com.fattiger.framework.cms.util.MenuUtil;

/**
 * Created by Administrator on 2015/12/1.
 */
@Controller
public class MenuController {

    @Autowired
    private MenuService menuService;

    @RequestMapping(value = "saveMenu.do", method = RequestMethod.POST)
    public String save(Menu menu, ModelMap map) {
        menuService.addMenu(menu);
        return menuList(null, map);
    }

    @RequestMapping(value = "menuList.do")
    public String menuList(Integer pid, ModelMap map) {

        List<Menu> menus;
        if (null != pid) {
            menus = menuService.getMenuByPid(pid);
        } else {
            menus = menuService.queryMenus();
            menus = MenuUtil.packMenu(menus);
        }
        map.put("menus", menus);
        return "menu_list";
    }

    @RequestMapping(value = "deleteMenu.do", method = RequestMethod.GET)
    public String deleteMenu(Integer id, ModelMap map) {
        menuService.deleteMenu(id);
        return menuList(null, map);
    }

    @RequestMapping(value = "getMenu.do", method = RequestMethod.GET)
    public String getMenu(Integer id, ModelMap map) {

        Menu menu = null;
        if (null != id) {
            menu = menuService.getMenu(id);
        }
        map.put("menu", menu);

        List<Menu> modules = menuService.getMenuByPid(-1);
        map.put("modules",modules);

        List<Menu> menuPool = menuService.queryMenus();
        map.put("menuPool",MenuUtil.packMenu(menuPool));

        return "menu_edit";
    }
}
